# -*- coding: utf-8 -*-
class RemindController < ApplicationController
  before_filter :sys_before_log

  #待审批销售订单提醒
  def checkSaleOrderAudit
    #先帮系统把要删的所有的过期的记录全删除掉
    SysMsg.delete_all("created_at < '#{2.day.ago}'")
    
    saleOrders = SaleOrder.find(:all,:conditions=>["auditing_status like ?","%#{Constants::APPLY}%"])
    #如果大于0，则向相关的用户角色发送信息
    if saleOrders.length > 0
      #查询系统信息注册表
      sysRemindRegisters = SysRemindRegister.find(:all,:conditions=>["name = '销售订单审批' AND is_register = 1"])
      sysRemindRegisters.each do |sysRemindRegister|
        employees = Employee.find(:all,:conditions=>["company_id = ? AND role_id = ?",sysRemindRegister.company_id,sysRemindRegister.role_id])
        employees.each do |employee|
          sysMsg = SysMsg.new
          sysMsg.company_id = employee.company_id
          sysMsg.employee_id = employee.id
          sysMsg.sub_type = "销售订单审批提醒"
          sysMsg.title = "有"+saleOrders.length.to_s+"条处于申请状态的销售订单，需要您审批。"
          sysMsg.content = "请在\"销售系统\"的\"销售订单审批\"中进行查询。"
          sysMsg.save!
        end
      end
    end
    jsonStr = "{success:true}"
    logger.info jsonStr
    render :text=>jsonStr, :layout=>false
  end

  #销售订单发货日期提醒，提前10天提醒
  def saleOrderDelivery
    #日期提前量
    afterDays = params[:days]
    if !afterDays.nil?
      afterDays = afterDays.to_i
    end
    #只要不是撤消的订单，只要没有出库完毕，就进行提醒。
    saleOrders = SaleOrder.find(:all,:conditions=>["store_status != ? AND auditing_status != '' AND auditing_status != ? AND action_date < ?",Constants::FINISH_DELIVERY,Constants::PASS_APPLY_CANCEL,Time.now + afterDays.days])
    #如果大于0，则向相关的用户角色发送信息
    if saleOrders.length > 0
      #查询系统信息注册表
      sysRemindRegisters = SysRemindRegister.find(:all,:conditions=>["name = '销售订单出库' AND is_register = 1"])
      sysRemindRegisters.each do |sysRemindRegister|
        employees = Employee.find(:all,:conditions=>["company_id = ? AND role_id = ?",sysRemindRegister.company_id,sysRemindRegister.role_id])
        employees.each do |employee|
          sysMsg = SysMsg.new
          sysMsg.company_id = employee.company_id
          sysMsg.employee_id = employee.id
          sysMsg.sub_type = "销售订单出货提醒"
          sysMsg.title = "有"+saleOrders.length.to_s+"条,处于"+params[:days]+"天内必须出库的销售订单，需要您审批。"
          sysMsg.content = "请在\"销售系统\"的\"销售订单\"中进行查询。"
          sysMsg.save!
        end
      end
    end
    jsonStr = "{success:true}"
    logger.info jsonStr
    render :text=>jsonStr, :layout=>false
  end


  #借货归还日期，借出的货，20天就要还，不还就提醒
  def loanReturned
    #日期提前量
    afterDays = params[:days]
    if !afterDays.nil?
      afterDays = afterDays.to_i
    end
    #只要不是撤消的订单，只要没有出库完毕，就进行提醒。
    loans = Loan.find(:all,:conditions=>["store_status != ? AND auditing_status != '' AND auditing_status != ? AND action_date < ?",Constants::FINISH_DELIVERY,Constants::PASS_APPLY_CANCEL,Time.now + afterDays.days])
    #如果大于0，则向相关的用户角色发送信息
    if loans.length > 0
      #查询系统信息注册表
      sysRemindRegisters = SysRemindRegister.find(:all,:conditions=>["name = '借货归还' AND is_register = 1"])
      sysRemindRegisters.each do |sysRemindRegister|
        employees = Employee.find(:all,:conditions=>["company_id = ? AND role_id = ?",sysRemindRegister.company_id,sysRemindRegister.role_id])
        employees.each do |employee|
          sysMsg = SysMsg.new
          sysMsg.company_id = employee.company_id
          sysMsg.employee_id = employee.id
          sysMsg.sub_type = "借货归还"
          sysMsg.title = "有"+loans.length.to_s+"条借货记录,已经借货超过"+params[:days]+"天，必须归还产品。"
          sysMsg.content = "请在\"借货系统\"的\"借货订单\"中进行查询。"
          sysMsg.save!
        end
      end
    end
    jsonStr = "{success:true}"
    logger.info jsonStr
    render :text=>jsonStr, :layout=>false
  end

  
  #库存提醒,当某个产品的数量大于最大，小于最小时就提醒。
  def productStock
    products = Product.find_by_sql("select name,min_quantity,max_quantity,pc.quantity from products p
left join (
select sum(quantity) as quantity ,product_id
from product_collections where is_locked = 0
group by product_id ) pc on pc.product_id = p.id
where ( pc.quantity > p.max_quantity and p.max_quantity <> 0 ) OR ( pc.quantity < p.min_quantity and p.min_quantity <> 0 )")
    if products.length > 0
      #查询系统信息注册表
      sysRemindRegisters = SysRemindRegister.find(:all,:conditions=>["name = '产品库存提醒' AND is_register = 1"])
      sysRemindRegisters.each do |sysRemindRegister|
        employees = Employee.find(:all,:conditions=>["company_id = ? AND role_id = ?",sysRemindRegister.company_id,sysRemindRegister.role_id])
        employees.each do |employee|
          sysMsg = SysMsg.new
          sysMsg.company_id = employee.company_id
          sysMsg.employee_id = employee.id
          sysMsg.sub_type = "产品库存提醒"
          sysMsg.title = "有"+products.length.to_s+"件产品多于或少于指定库存数，请于‘采购管理决策分析’中进行查询详细。"
          sysMsg.content = "有"+products.length.to_s+"件产品多于或少于指定库存数，请于‘采购管理决策分析’中进行查询详细。"
          sysMsg.save!
        end
      end
    end
  end
end
